<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>下拉刷新</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 90%;
            height: 100vh;
            background-color: antiquewhite;
            margin: 0 auto;
        }

        .circle {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: #e74c3c;
            box-shadow: 1px 1px 1px #BFBFBF;
            position: absolute;
            top: -50px;
            left: 50%;
            transform: translateX(-50%);
            transition: opacity, top .2s ease-in-out;
        }
        img{
            width:70%;
            margin-left: 5px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div class="box"></div>
<div class="circle">
    <img src="./images/circle.png" alt="">
</div>
<script>
    let oCircle = document.querySelector('.circle');
    let lastY = 0;
    let canMove = false;
    let distance = 0;
    document.addEventListener('touchstart', (e) => {
        e.preventDefault();
        oCircle.style.transition = 'opacity .2s ease-in-out'
        oCircle.style.opacity = '1';
        canMove = true;
        console.log(e.target);
        let touch = e.touches[0];
        // console.log(touch.clientY)
        lastY = touch.clientY;
        console.log(lastY);
    }, {passive: false});
    document.addEventListener('touchmove', (e) => {
        if (canMove) {
            let touch = e.touches[0];
            distance = touch.clientY - lastY;
            console.log(distance)
            // if (distance < 0) return;
            if (distance > 100) {
                return;
            }
            oCircle.style.top = distance + 'px';
        }
    }, false);
    document.addEventListener('touchend', (e) => {
        if (distance > 100) {
            setTimeout(function () {
                console.log('数据加载完毕')
                oCircle.style.opacity = '0';
                oCircle.style.top = '-50px';
            }, 1000)
        } else {
            oCircle.style.transition = 'top .2s ease-in-out'
            oCircle.style.top = '-50px';
        }
    }, false);
</script>
</body>
</html>
